﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AccessControl.Contracts.Models;
using AccessControl.Data.Models;

namespace AccessControl.Wpf.Controls
{
	/// <summary>
	/// Interaction logic for MapControl.xaml
	/// </summary>
	public partial class MapControl : UserControl
	{
		#region Fields

		private static readonly Type s_ThisType = typeof(MapControl);

		#endregion

		#region Public Consttuctor

		public MapControl()
		{
			InitializeComponent();
		}

		#endregion

		#region Dependency Properties

		public static readonly DependencyProperty MapImageProperty = DependencyProperty.Register(
			"MapImage",
			typeof(ImageSource),
			s_ThisType,
			new PropertyMetadata(null, OnMapImageChanged));

		public static readonly DependencyProperty MapEntriesProperty = DependencyProperty.Register(
			"MapEntries",
			typeof(IList<IMapEntry>),
			s_ThisType,
			new PropertyMetadata(null));

		public static readonly DependencyProperty IsInEditModeProperty = DependencyProperty.Register(
			"IsInEditMode",
			typeof(bool),
			s_ThisType,
			new PropertyMetadata(false));

		public static readonly DependencyProperty LeftOffsetProperty = DependencyProperty.Register(
			"LeftOffset",
			typeof(double),
			s_ThisType,
			new PropertyMetadata(0.00));

		public static readonly DependencyProperty TopOffsetProperty = DependencyProperty.Register(
			"TopOffset",
			typeof(double),
			s_ThisType,
			new PropertyMetadata(0.00));

		public static readonly DependencyProperty VisibleWidthProperty = DependencyProperty.Register(
			"VisibleWidth",
			typeof(double),
			s_ThisType,
			new PropertyMetadata(0.00));

		public static readonly DependencyProperty VisibleHeightProperty = DependencyProperty.Register(
			"VisibleHeight",
			typeof(double),
			s_ThisType,
			new PropertyMetadata(0.00));

		#endregion

		#region Public Properties

		public ImageSource MapImage
		{
			get { return (ImageSource)GetValue(MapImageProperty); }
			set { SetValue(MapImageProperty, value); }
		}

		public IList<IMapEntry> MapEntries
		{
			get { return (IList<IMapEntry>)GetValue(MapEntriesProperty); }
			set { SetValue(MapEntriesProperty, value); }
		}

		public bool IsInEditMode
		{
			get { return (bool)GetValue(IsInEditModeProperty); }
			set { SetValue(IsInEditModeProperty, value); }
		}

		public double LeftOffset
		{
			get { return (double)GetValue(LeftOffsetProperty); }
			set { SetValue(LeftOffsetProperty, value); }
		}

		public double TopOffset
		{
			get { return (double)GetValue(TopOffsetProperty); }
			set { SetValue(TopOffsetProperty, value); }
		}

		public double VisibleWidth
		{
			get { return (double)GetValue(VisibleWidthProperty); }
			set { SetValue(VisibleWidthProperty, value); }
		}

		public double VisibleHeight
		{
			get { return (double)GetValue(VisibleHeightProperty); }
			set { SetValue(VisibleHeightProperty, value); }
		}
		
		#endregion

		#region Overrides

		protected override void OnDrop(DragEventArgs e)
		{
			Point ptRelative;
			double xPos, yPos, left, top;

			base.OnDrop(e);

			/* Make sure we are in edit mode */
			if (!IsInEditMode)
				return;

			if (e.Effects == DragDropEffects.None)
				return;

			if (e.Data.GetDataPresent(typeof(Entry)))
			{
				/* Get the dragged entry */
				Entry draggedEntry = (Entry)e.Data.GetData(typeof(Entry));
				/* Check if we don't have it already */
				foreach (IMapEntry entry in MapEntries)
				{
					if (entry.Id == draggedEntry.Id)
					{
						e.Effects = DragDropEffects.None;
						return;
					}
				}
				/* Calc the location */
				ptRelative = e.GetPosition(_itemsControl);
				left = (double)_itemsControl.GetValue(Canvas.LeftProperty);
				top = (double)_itemsControl.GetValue(Canvas.TopProperty);
				xPos = left + ptRelative.X;
				yPos = top + ptRelative.Y;
				/* Create new map one */
				MapEntry mapEntry = new MapEntry()
				{
					Id = draggedEntry.Id,
					Name = draggedEntry.Name,
					PositionOnMap = new Position()
					{
						X = xPos,
						Y = yPos
					}
				};

				/* And add to the map items */
				if (MapEntries != null)
					MapEntries.Add(mapEntry);
			}
		}

		#endregion

		#region Private Members

		private static void OnMapImageChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{

		}

		#endregion
	}
}
